<?php
namespace App\Api\GoModule\Controllers;
use Log;
use App\Api\Controllers\BaseController;
use App\Http\Requests\checkDriverLocation;
use App\Wechat;
use App\OrderRecord;
use App\DeliveryRecord;
use Illuminate\Http\Request;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
class GoController extends BaseController
{
    /**
     * The authentication guard that should be used.
     *
     * @var string
     */
    public function __construct()
    {
        parent::__construct();

    }


    /**
     *<author johannlai@outlook.com>
     *@ function to check if the order is accept
     *@ params token
     *@ return if accepted return the the driver's infomation
     *        else return not be accepted
     */
    //Driver check if his location has orders
    public function userCheckOrder(Request $request)
    {
        //get Token
        $token = JWTAuth::getToken();

        //get the user infomation
        $user_json = JWTAuth::toUser($token);

        $user = json_decode($user_json, true);
	$userphone = $user['tel'];

	//检测送货订单
	$orders=[];
	$checkDelis = Redis::sinter('send:'.$userphone);
	if($checkDelis)
	foreach($checkDelis as $orderNum){
		$query = Redis::hgetall($orderNum);
		if($query){
		$isAccept = $query['isAccept'];
		switch($isAccept)	{
	  		case 0:
          		  return $this->returnMsg('200', 'ok', ['status'=>'00','orderInfo'=>$query]);
  	 		case 1:
 	  		  $driverPhone = $query['driverPhone'];
	  		  $driverInfo = $this->getDriverInfo($driverPhone);
              		  return $this->returnMsg('200', 'ok', ['status'=>'01','orderInfo'=>$query,'driverInfo'=>$driverInfo]);
  	  		case 2:
 	  		  $driverPhone = $query['driverPhone'];
	  		  $driverInfo = $this->getDriverInfo($driverPhone);
              		  return $this->returnMsg('200', 'ok', ['status'=>'02','orderInfo'=>$query,'driverInfo'=>$driverInfo]);
			case 3:
 	  		  $driverPhone = $query['driverPhone'];
	  		  $driverInfo = $this->getDriverInfo($driverPhone);
              		  return $this->returnMsg('200', 'ok', ['status'=>'03','orderInfo'=>$query,'driverInfo'=>$driverInfo]);
			/*case 4:
 	  		  $driverPhone = $query['driverPhone'];
	  		  $driverInfo = $this->getDriverInfo($driverPhone);
              		  return $this->returnMsg('200', 'ok', ['status'=>'04','orderInfo'=>$query,'driverInfo'=>$driverInfo]);*/
	  		default:
			  break;
			}
		  }
		}

	//检测送人订单
         $checkredis = Redis::exists('usecar:'.$userphone);
         $check_order_mysql3 = OrderRecord::where('userphone','=',$userphone)->where('isAccept','=',3)->first();
         $check_order_mysql4 = OrderRecord::where('userphone','=',$userphone)->where('isAccept','=',4)->first();
	 
         if($checkredis)//若redis有数据则说明有订单正在进行
         {
                 $query = Redis::hgetall('usecar:'.$userphone);
		 $isAccept = $query['isAccept'];
		 switch($isAccept)	{
	  		case 0:
          		  return $this->returnMsg('200', 'ok', ['status'=>'0','orderInfo'=>$query]);
  	 		case 1:
 	  		  $driverPhone = $query['driverPhone'];
	  		  $driverInfo = $this->getDriverInfo($driverPhone);
              		  return $this->returnMsg('200', 'ok', ['status'=>'1','orderInfo'=>$query,'driverInfo'=>$driverInfo]);
  	  		case 2:
 	  		  $driverPhone = $query['driverPhone'];
	  		  $driverInfo = $this->getDriverInfo($driverPhone);
              		  return $this->returnMsg('200', 'ok', ['status'=>'2','orderInfo'=>$query,'driverInfo'=>$driverInfo]);
	  		default:
			  break;
			}
         }
          if($check_order_mysql3){//未付款
			$driverPhone = $check_order_mysql3->driverPhone;
	  		$driverInfo = $this->getDriverInfo($driverPhone);
              		return $this->returnMsg('200', 'ok', ['status'=>'3','orderInfo'=>$check_order_mysql3,'driverInfo'=>$driverInfo]);
         	}
	  if($check_order_mysql4){//未评论
			$driverPhone = $check_order_mysql4->driverPhone;
	  		$driverInfo = $this->getDriverInfo($driverPhone);
              		return $this->returnMsg('200', 'ok', ['status'=>'4','orderInfo'=>$check_order_mysql4,'driverInfo'=>$driverInfo]);
		}
	 
		return $this->returnMsg('200','ok',['status'=>'-1']);
    }

    /*
     *@author johannlai
     *@function to get the driver's Location
     *@params driver's phone
     *
     */
    public function checkDriverLocation(Request $request)
    {
        $token = JWTAuth::getToken();
        $user_json = JWTAuth::toUser($token);
        $user = json_decode($user_json, true);
        return $user['tel'];
    }

      /*
     *@author johannlai
     *@function to get the orderStatus
     *@params driver's phone
     *
     */
    public function checkOrderStatus(Request $request)
    {
        $token = JWTAuth::getToken();
        $user_json = JWTAuth::toUser($token);
        $user = json_decode($user_json, true);
	$status = Redis::hget('usecar:'.$user['tel'],"isAccept");
        
	if($status == ''){
         $query = OrderRecord::where(['isAccept'=>'3', 'userphone'=>$user['tel']])->first();//获取未支付
	if($query){
	 Log::info('order info:'.$query->driverPhone);
           //the status of the Order
            $result = $this->result('200', 'ok', ['status'=>'3']);
            return response()->json($result);
        } else{
		$query = OrderRecord::where(['isAccept'=>'4', 'userphone'=>$user['tel']])->first();//获取待评价订单
		if(!$query){
	  		$result = $this->result('200', 'ok',['status'=>'-1']);
	 		  return response()->json($result);}
		else {
			$result = $this->result('200','ok', ['status'=>'4']);
			return response()->json($result);
		}
	} 
	}
        $result = $this->result('200', 'ok',['status'=>$status]);
	return response()->json($result);
    	
    }

    /*
     *@funtion : to build the reponse infomation
     *
     */
    public function result ( $code=200, $message="ok", $data=null )
    {
        $result['code']= $code;
        $result['message']= $message;
        $result['data']= $data;
        return $result;
    }
}
